package cn.jinjimi.app.main.widget.biz;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.entity.Dynamic;
import cn.jinjimi.app.entity.HeadLine;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by linlongjie on 17/11/16.
 */

public class HomeHeadDynamicView extends LinearLayout implements View.OnClickListener {
    private RelativeLayout rlContainer;
    private LinearLayout llStatus;
    private TextView tvStatus;
    private ImageView ivStatus, ivDynamic;
    private TextSwitcher tsDynamic;
    private View vLine;

    private HeadLine mHeadLine;
    private List<Dynamic> mDynamics = new ArrayList<>();
    private int mStatus = -1;

    private List<String> mStrings = new ArrayList<>();
    private int mDelayTime = 3000;
    private int mIndex = 0;
    private int mCurrentPosition = 0;

    private Handler mHandler = new Handler();
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            if (mHandler == null || mStrings == null || mStrings.isEmpty() || mStrings.size() < 2)
                return;
            mCurrentPosition = mIndex % mStrings.size();
            tsDynamic.setText(mStrings.get(mCurrentPosition));
            mIndex++;
            mHandler.postDelayed(this, mDelayTime);
        }
    };

    public HomeHeadDynamicView(Context context) {
        this(context, null, 0);
    }

    public HomeHeadDynamicView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HomeHeadDynamicView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.view_home_head_dynamic, this);
        initView();
    }

    private void initView() {
        rlContainer = (RelativeLayout) findViewById(R.id.rl_container);
        llStatus = (LinearLayout) findViewById(R.id.rl_status);
        ivStatus = (ImageView) findViewById(R.id.iv_status);
        tvStatus = (TextView) findViewById(R.id.tv_status);
        ivDynamic = (ImageView) findViewById(R.id.iv_dynamic);
        tsDynamic = (TextSwitcher) findViewById(R.id.ts_dynamic);
        vLine = findViewById(R.id.interval_line);

        tsDynamic.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView textView = new TextView(getContext());
                textView.setSingleLine();
                textView.setTextSize(13);
                textView.setTextColor(getResources().getColor(R.color.text_low_black));
                textView.setEllipsize(TextUtils.TruncateAt.END);
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                        FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.gravity = Gravity.CENTER_VERTICAL;
                textView.setLayoutParams(params);
                return textView;
            }
        });

        tvStatus.setOnClickListener(this);
        tsDynamic.setOnClickListener(this);
    }

    public void setData(List<Dynamic> dynamicList) {
        mDynamics.clear();
        mStrings.clear();
        mIndex = 0;
        mCurrentPosition = 0;
        if (mHandler != null && mRunnable != null) {
            mHandler.removeCallbacks(mRunnable);
        }

        if (dynamicList == null || dynamicList.isEmpty()) {
            ivDynamic.setVisibility(INVISIBLE);
            tsDynamic.setVisibility(INVISIBLE);
            return;
        }
        getValidDynamics(dynamicList);
        if (mDynamics.isEmpty()) {
            return;
        }
        rlContainer.setVisibility(View.VISIBLE);
        ivDynamic.setVisibility(VISIBLE);
        tsDynamic.setVisibility(VISIBLE);
        if (mStrings.size() < 2) {
            tsDynamic.setText(mStrings.get(0));
        } else {
            mHandler.post(mRunnable);
        }
    }

    public void setStatus(HeadLine headLine) {
        if (headLine == null) {
            llStatus.setVisibility(GONE);
            vLine.setVisibility(GONE);
            return;
        }
        mHeadLine = headLine;
        int status = headLine.status;
        mStatus = status;
        if (status == -1) {
            llStatus.setVisibility(GONE);
        } else if (status == 1) {
            rlContainer.setVisibility(View.VISIBLE);
            llStatus.setVisibility(VISIBLE);
            ivStatus.setImageResource(R.drawable.ico_index_sign);
            tvStatus.setText(getResources().getText(R.string.home_sign_status_yes));
        } else if (status == 0) {
            rlContainer.setVisibility(View.VISIBLE);
            llStatus.setVisibility(VISIBLE);
            ivStatus.setImageResource(R.drawable.ico_index_unsign);
            tvStatus.setText(getResources().getText(R.string.home_sign_status_no));
        }
        vLine.setVisibility(llStatus.getVisibility());
    }

    public void hide() {
        rlContainer.setVisibility(View.GONE);
    }

    private void getValidDynamics(List<Dynamic> dynamicList) {
        for (Dynamic dynamic : dynamicList) {
            if (dynamic == null || StringUtils.isEmpty(dynamic.title)) continue;
            mDynamics.add(dynamic);
            mStrings.add(dynamic.title);
        }
    }

    @Override
    public void onClick(View v) {

    }
}
